﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows;

namespace XMM.Objects
{
    public class Audition : Object
    {
        /// <summary>
        /// Database-Record ID.
        /// </summary>
        public int AuditionID { get; set; }

        /// <summary>
        /// Audition-Name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Last login of this audition or NULL.
        /// </summary>
        public DateTime LastLogin { get; set; }

        public PlaybackHistoryRecord LastPlayback
        {
            get
            {
                PlaybackHistoryRecord oRecord = Objects.PlaybackHistoryRecord.getLastPlaybackRecords(new Objects.Audition(AuditionID));

                return oRecord;
            }
        }

        /// <summary>
        /// Type of last played media.
        /// </summary>
        /// <remarks>0...none or unknown
        /// 1...Movie
        /// 2...Episode
        /// 3...Audio-File
        /// 4...Picture</remarks>
        public int LastMediaType { get; set; }

        /// <summary>
        /// Database-Record ID of the last played media.
        /// </summary>
        public int LastMediaID { get; set; }

        /// <summary>
        /// Log this auditions playback-history.
        /// </summary>
        public bool LogHistory { get; set; }

        /// <summary>
        /// Initialize a new Audition-Object.
        /// </summary>
        public Audition()
        {
            blankValues();
        }

        /// <summary>
        /// Blank all values.
        /// </summary>
        private void blankValues()
        {
            AuditionID = 0;
            Name = String.Empty;
            LastLogin = DateTime.MinValue;
            LastMediaType = 0;
            LastMediaID = 0;
            LogHistory = false;
        }

        /// <summary>
        /// Initialize a new Audition-Object and load info from database.
        /// </summary>
        /// <param name="iAuditionID">(int) Target-Audition Database-ID.</param>
        public Audition(int iAuditionID)
        {
            blankValues();

            Load(iAuditionID);
        }

        /// <summary>
        /// Load Audition-Info from database.
        /// </summary>
        /// <param name="iAuditionID">(int) Target-Audition Database-ID.</param>
        /// <returns>[true] if record was loaded successfully.</returns>
        public bool Load(int iAuditionID)
        {
            blankValues();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();
                var vAuditions = from audition in dbConnection.t_auditions where audition.ID == iAuditionID select audition;

                if (vAuditions != null)
                {
                    foreach (t_auditions vAudition in vAuditions)
                    {
                        AuditionID = vAudition.ID;
                        Name = vAudition.Name;
                        if (vAudition.LastLogin != null)
                        {
                            LastLogin = (DateTime)vAudition.LastLogin;
                        }
                        if(vAudition.LastType != null)
                            LastMediaType = (int)vAudition.LastType;

                        if(vAudition.LastID != null)
                            LastMediaID = (int)vAudition.LastID;

                        if (vAudition.LogHistory == 0)
                            LogHistory = false;
                        else
                            LogHistory = true;
                    }

                    return true;
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Laden der Zuseher-Info: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return false;
        }

        /// <summary>
        /// Get a list off all available audition-presets.
        /// </summary>
        /// <returns>[List(Audition)] Available auditions.</returns>
        public static List<Audition> getAllAuditions()
        {
            List<Audition> lAuditions = new List<Audition>();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();
                var vAuditionIDs = from auditionIDs in dbConnection.t_auditions select auditionIDs.ID;

                if (vAuditionIDs != null)
                {
                    foreach (int iAuditionID in vAuditionIDs)
                    {
                        if (iAuditionID != 0)
                        {
                            Audition oAudition = new Audition(iAuditionID);
                            if (oAudition != null && oAudition.AuditionID != 0)
                            {
                                lAuditions.Add(oAudition);
                            }
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Laden der verfügbaren Zuseher: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return lAuditions;
        }

        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// Save new Audition.
        /// </summary>
        /// <returns>[true] if Audition was saved successfully.</returns>
        public bool SaveNew()
        {
            if (Name != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_auditions oAudition = new t_auditions();
                    oAudition.Name = Name;
                    if (LogHistory)
                        oAudition.LogHistory = 1;
                    else
                        oAudition.LogHistory = 0;

                    dbConnection.t_auditions.InsertOnSubmit(oAudition);
                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Speichern des Zusehers: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

        /// <summary>
        /// Save changes to existing Audition.
        /// </summary>
        /// <returns>[true] if changes where saved successfully otherwhise [false].</returns>
        public bool SaveChanges()
        {
            if (AuditionID != 0 && Name != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    var vAudition = (from audition in dbConnection.t_auditions where audition.ID == AuditionID select audition).Single();

                    if (vAudition != null)
                    {
                        vAudition.Name = Name;
                        if (LogHistory)
                            vAudition.LogHistory = 1;
                        else
                            vAudition.LogHistory = 0;

                        dbConnection.SubmitChanges();
                        return true;
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Speichern der Änderungen an Zuseher \"" + Name + "\": " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

        public static bool Delete(int iAuditionID)
        {
            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vTargetAudience = from audience in dbConnection.t_auditions where audience.ID == iAuditionID select audience;

                if (vTargetAudience != null)
                {
                    dbConnection.t_auditions.DeleteOnSubmit(vTargetAudience.Single());
                    dbConnection.SubmitChanges();
                    return true;
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Löschen von Zuseher '" + iAuditionID.ToString() + "': " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return false;
        }
    }
}
